package com.lpf.www.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.ByteArrayInputStream;

/**
 * @Author lpf
 * @Date 2022/1/6 上午9:00
 * @ProjectName live-streaming-os
 * @Version 1.0
 * @Description:
 */
public class OssUtils {

    private static String imageName = "image/";
    private static String videoName = "video/";

    private static OSS ossClient = null;

    /**
     * 初始化ossClient对象
     */
    static {
        ossClient = new OSSClientBuilder().build(CommonData.ENDPOINT, CommonData.ACCESS_KEY_ID, CommonData.ACCESS_KEY_SECRET);
    }

    public static String uploadImg(String filename, byte[] bytes) {
        ossClient.putObject(CommonData.BUCKET_NAME, imageName + "/" + filename, new ByteArrayInputStream(bytes));
        return "http://" + CommonData.BUCKET_NAME + "." + CommonData.ENDPOINT + "/" + filename;
    }

    public static String uploadVideo(String filename, byte[] bytes){
        ossClient.putObject(CommonData.BUCKET_NAME, videoName + "/" + filename, new ByteArrayInputStream(bytes));
        return "http://" + CommonData.BUCKET_NAME + "." + CommonData.ENDPOINT + "/" + filename;
    }

}